package com.slj.springcloud.controller;

import com.slj.springcloud.model.dto.PayDTO;
import com.slj.springcloud.model.entity.PayEntity;
import com.slj.springcloud.model.response.Result;
import com.slj.springcloud.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/pay")
@RefreshScope
public class PayController {
    @Autowired
    private PayService payService;

    @Autowired
    private Environment environment;

    @Value("${name}")
    private String name;

    @GetMapping("")
    public Result<List<PayDTO>> findAll() {
        return Result.success(payService.findAll());
    }

    @GetMapping("/port")
    public Result<String> getPort() {
        String port = "";
        if (environment.containsProperty("server.port")) {
            port = environment.getProperty("server.port");
        }
        return Result.success(name + ":=" + port);
    }

    @GetMapping("/timeout")
    public Result<String> timeout() throws InterruptedException {
        TimeUnit.SECONDS.sleep(6);
        return Result.success("time out");
    }

}
